﻿#region Usings
using System.Collections.Generic;
using System.Linq;

using BK.Model;
#endregion

namespace BK.Repositories.EF
{
    public class TreeNodeRepositoryBase<TTreeNode> : BaseRepository<TTreeNode>
        where TTreeNode : UserDependedTreeNodeEntity<TTreeNode>, IUserDependedEntity
    {
        #region C'tors
        public TreeNodeRepositoryBase(BkDataContext dataContext)
            : base(dataContext)
        {
        }
        #endregion

        #region Instance Methods
        public TTreeNode GetBy(int userId, string name, int? parentId)
        {
            if (parentId != null)
            {
                return (from entity in ObjectSet.OfType<TTreeNode>()
                        where (entity.User.Id == userId) && (entity.Name == name)
                            && (entity.ParentId == parentId)
                        select entity).FirstOrDefault();
            }

            return (from entity in ObjectSet.OfType<TTreeNode>()
                    where (entity.User.Id == userId) && (entity.Name == name)
                        && (entity.ParentId == null)
                    select entity).FirstOrDefault();
        }

        public IEnumerable<TTreeNode> GetBy(int userId)
        {
            return (from entity in ObjectSet.OfType<TTreeNode>() 
                    where entity.User.Id == userId 
                    select entity).ToList();
        }
        #endregion
    }
}